package com.bw.mallshop.mvvm.viewmodel;

import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mallshop.MainActivity;
import com.bw.mallshop.mvvm.model.SplashModel;
import com.bw.mallshop.mvvm.model.entity.TokenEntity;
import com.bw.mallshop.timmer.TimeHandler;
import com.bw.mallshop.timmer.TimeThread;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.SPUtils;

import java.util.HashMap;
import java.util.logging.Handler;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/13 16:18
 */
public class SplashViewModel extends BaseViewModel<SplashModel> {

    public ObservableField<String> timeObserable = new ObservableField<>("3s");
     TimeHandler timeHandler;
    @Override
    public void oncreate() {
        super.oncreate();
        //页面加载完成后
        //请求token
        requestToken();
        timeHandler = new TimeHandler(this);
    }

    private void requestToken(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

    }
    @Override
    protected void result(BaseEntity entity) {
        //判断返回值状态是否请求成功
        if(entity.getMessage().equals("imie为空")){
            showMsg(entity.getMessage());
            return;
        }
        if(entity.getMessage().equals("sign为空")){
            showMsg(entity.getMessage());
            return;
        }
        TokenEntity tokenEntity = (TokenEntity) entity;

        //成功token保存本地
        SPUtils.writeToken(tokenEntity.getValues().token);
        //启动定时线程定时3s
        new TimeThread(timeHandler).start();

    }

    //跳过
    public void skipTimer(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, MainActivity.class);
        startPage(map);
    }
}
